package phoenix.web.test_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.test_objects.data_providers.PhotoGalleryDataProviders;

/**
 * Created by AlexGolovko on 26.11.2014.
 */
public class PhotoGalleryTests extends TestBaseWebExt
{
	public PhotoGalleryTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public PhotoGalleryDataProviders dataProviders = new PhotoGalleryDataProviders(manager);

	/**
	 * TODO use photo number from DB
	 */
	public void openGalleryTest(TestUserDataModel p_user)
	{
		try
		{
			// Login
			wd().openAutologin(testUser());
			// Go to static profile
			wd().openPage("/user/view/id/" + p_user.getId());
			page().userPage().waitForPageLoad();
			page().galleryPage().clickUserPhoto();
			page().galleryPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("openGalleryTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	/**
	 * TODO use photo number from DB
	 */
	public void photosGalleryTest(TestUserDataModel p_user, Integer p_expectedNumPhotos)
	{
		try
		{
			this.openGalleryTest(p_user);
			// Show all photos
			if (manager.isMD())
			{
				page().galleryPage().clickUserPhoto();
			}
			else // Otther
			{
				page().galleryPage().clickAllPhotos();
			}

			wd().wait(1); // Required for js load
			Integer number_photos = page().galleryPage().getPhotoCount();

			test().assertEqualsWithScreen(number_photos, p_expectedNumPhotos, "photo images in gallery");
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("photosGalleryTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public void navigationGalleryTest(TestUserDataModel p_user)
	{
		try
		{
			String p_idOfPhotoActual = page().galleryPage().getPhotoId();

			this.openGalleryTest(p_user);
			/**
			 * Test for next button
			 */
			// Show all photos
			page().galleryPage().clickAllPhotos();
			wd().wait(2); // Required for js load

			// Click navigation go to second photo
			p_idOfPhotoActual = this.goNextTest(p_idOfPhotoActual);

			// Click navigation go to third photo
			p_idOfPhotoActual = this.goNextTest(p_idOfPhotoActual);

			// Click navigation go to first photo
			p_idOfPhotoActual = this.goNextTest(p_idOfPhotoActual);

			/**
			 * Test for previous button
			 */
			// Click navigation go to third photo
			p_idOfPhotoActual = this.goPrevTest(p_idOfPhotoActual);

			// Click navigation go to second photo
			p_idOfPhotoActual = this.goPrevTest(p_idOfPhotoActual);

			// Click navigation go to first photo
			p_idOfPhotoActual = this.goPrevTest(p_idOfPhotoActual);

			// Close gallery
			page().galleryPage().clickCloseGallery();
			page().userPage().waitForPageLoad();

		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("navigationGalleryTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public String goNextTest(String p_idPreviousPhoto)
	{
		String p_idOfPhotoActual = "";
		try
		{
			// Click navigation go to first photo
			page().galleryPage().clickNextPhoto();
			wd().wait(2); // Required for js load
			// Do test
			p_idOfPhotoActual = page().galleryPage().getPhotoId();
			log().info("Current photo id " + p_idOfPhotoActual);
			test().assertNotEqualsWithScreen(p_idOfPhotoActual, p_idPreviousPhoto, "Compare photo ids");

		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("goNextTest " + p_ex.toString());
		}
		return p_idOfPhotoActual;
	}

	public String goPrevTest(String p_idPreviousPhoto)
	{
		String p_idOfPhotoActual = "";
		try
		{
			// Click navigation go to first photo
			page().galleryPage().clickPrevPhoto();
			wd().wait(2); // Required for js load
			// Do test
			p_idOfPhotoActual = page().galleryPage().getPhotoId();
			log().info("Current photo id " + p_idOfPhotoActual);
			test().assertNotEqualsWithScreen(p_idOfPhotoActual, p_idPreviousPhoto, "Compare photo ids");
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("goNextTest " + p_ex.toString());
		}
		return p_idOfPhotoActual;
	}
}
